package com.zerowidth.moc.web.controller;

import com.zerowidth.moc.web.auth.annotation.NoToken;
import com.zerowidth.moc.web.resp.ResultBean;
import com.zerowidth.moc.web.resp.helper.ResponseCreator;
import com.zerowidth.moc.web.utils.IpUtil;
import com.zerowidth.db.op.LogDB;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1")
public class IPController {

    /**
     * 用于测试客户端请求的公网地址，检查代理是否成功
     * 请求不需要签名
     */
    @NoToken
    @GetMapping("/ip_test_no_sign")
    public ResponseEntity<ResultBean<String>> getIpNoSign(HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);

        LogDB.insert("查询外网ip地址成功= " + ip);
        return ResponseCreator.creatOk(ip, 0, "获取外网ip成功");
    }

    /**
     * 用于测试客户端请求的公网地址，检查代理是否成功
     * 请求需要签名
     */
    @NoToken
    @GetMapping("/ip_test_with_sign")
    public ResponseEntity<ResultBean<String>> getIpWithSign(HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);

        LogDB.insert("客户端的ip地= " + ip);
        return ResponseCreator.creatOk(ip, 0, "获取外网ip成功");
    }

}
